package top.codedance.iotp.common.entity;

import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.lang.reflect.Field;

public class ServerMeta implements Serializable {
    private String bindIp;
    private String httpBindHost;
    private String wsBindHost;
    private int tcpPort;
    private int httpPort;
    private int wsPort;
    private String netUsername;
    private String netPassword;
    private int weight;

    public ServerMeta() {}

    public ServerMeta(String bindIp, String httpBindHost, String wsBindHost, int tcpPort, int httpPort, int wsPort, String netUsername, String netPassword) {
        this.bindIp = bindIp;
        this.httpBindHost = httpBindHost;
        this.wsBindHost = wsBindHost;
        this.tcpPort = tcpPort;
        this.httpPort = httpPort;
        this.wsPort = wsPort;
        this.netUsername = netUsername;
        this.netPassword = netPassword;
        this.weight = 0;
    }

    public ServerMeta(String bindIp, String httpBindHost, String wsBindHost, int tcpPort, int httpPort, int wsPort, String netUsername, String netPassword, int weight) {
        this.bindIp = bindIp;
        this.httpBindHost = httpBindHost;
        this.wsBindHost = wsBindHost;
        this.tcpPort = tcpPort;
        this.httpPort = httpPort;
        this.wsPort = wsPort;
        this.netUsername = netUsername;
        this.netPassword = netPassword;
        this.weight = weight;
    }

    public String getBindIp() {
        return bindIp;
    }

    public void setBindIp(String bindIp) {
        this.bindIp = bindIp;
    }

    public String getHttpBindHost() {
        return httpBindHost;
    }

    public void setHttpBindHost(String httpBindHost) {
        this.httpBindHost = httpBindHost;
    }

    public String getWsBindHost() {
        return wsBindHost;
    }

    public void setWsBindHost(String wsBindHost) {
        this.wsBindHost = wsBindHost;
    }

    public int getTcpPort() {
        return tcpPort;
    }

    public void setTcpPort(int tcpPort) {
        this.tcpPort = tcpPort;
    }

    public int getHttpPort() {
        return httpPort;
    }

    public void setHttpPort(int httpPort) {
        this.httpPort = httpPort;
    }

    public int getWsPort() {
        return wsPort;
    }

    public void setWsPort(int wsPort) {
        this.wsPort = wsPort;
    }

    public String getNetUsername() {
        return netUsername;
    }

    public void setNetUsername(String netUsername) {
        this.netUsername = netUsername;
    }

    public String getNetPassword() {
        return netPassword;
    }

    public void setNetPassword(String netPassword) {
        this.netPassword = netPassword;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public JSONObject getJSONObject(){
        JSONObject jsonObject = new JSONObject();
        Field[] fields = getClass().getDeclaredFields();
        for (Field f : fields){
            try {
                f.setAccessible(true);
                jsonObject.put(f.getName(), f.get(this));
            }catch(Exception e){}
        }
        return jsonObject;
    }

    @Override
    public String toString() {
        return getJSONObject().toJSONString();
    }

    public static ServerMeta parse(String jsonString){
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        return new ServerMeta(
                jsonObject.getString("bindIp"),
                jsonObject.getString("httpBindHost"),
                jsonObject.getString("wsBindHost"),
                jsonObject.getIntValue("tcpPort"),
                jsonObject.getIntValue("httpPort"),
                jsonObject.getIntValue("wsPort"),
                jsonObject.getString("netUsername"),
                jsonObject.getString("netPassword"),
                jsonObject.getIntValue("weight")
        );
    }
}
